package datagramSocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class LogicThread extends Thread{
	DatagramSocket ds = null;
	DatagramPacket dp;   //接收的数据
	
	
	public LogicThread(DatagramSocket ds, DatagramPacket dp){
		this.ds = ds;
		this.dp = dp;
		
		start();
	}
	
	public void run(){
		try{
			//获得缓存数组
			byte[] data = dp.getData();
			int len = dp.getLength();
			InetAddress clientAddress = dp.getAddress();
			int clientPort = dp.getPort();
			
			System.out.println(" 收到来自 :" + clientAddress.getAddress() + ":" + clientPort + " 的数据");
			System.out.println( new String( data, 0, len));
			
			byte[] b = "OK".getBytes();
			DatagramPacket sendDp = new DatagramPacket(b, b.length, clientAddress, clientPort);
			
			ds.send(sendDp);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
